package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;
    /**  插入预约表
     *
     *  /ordersetting/upload.do
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam(value = "excelFile") MultipartFile excelFile){
        try {
//            1.解析获得每一行数据
            List<String[]> list= POIUtils.readExcel(excelFile);
            if (list.size()>0 && list!=null){
                List<OrderSetting> orderSettingList=new ArrayList<>();
                for (String[] strings:list){
                    OrderSetting orderSetting = new OrderSetting(new SimpleDateFormat("yyyy/MM/dd").parse(strings[0]), Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
               orderSettingService.add(orderSettingList);
            }
//            成功批量插入数据
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }


    /**
     *   根据日期范围--查找预约数据
     * /ordersetting/findAllOrderSettingByBate?date="+this.currentYear+"-"+this.currentMonth
     */
    @RequestMapping("/findAllOrderSettingByBate")
    public Result findAllOrderSettingByBate(String date){
        try{
            List<Map> map=orderSettingService.findAllOrderSettingByBate(date);
            return  new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     *  根据日期更新可预约人数
     *  /ordersetting/updateOrderSettingNumber",data
     */
    @RequestMapping ("/updateOrderSettingNumber")
    public Result updateOrderSettingNumber(@RequestBody OrderSetting orderSetting){
        try{
            orderSettingService.updateOrderSettingNumber(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            return  new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
